package com.doublecheck.project.client;

import java.util.ArrayList;


import com.google.gwt.user.client.ui.Widget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.chart.client.chart.Chart;
import com.sencha.gxt.chart.client.chart.axis.RadialAxis;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent;
import com.sencha.gxt.chart.client.chart.event.SeriesSelectionEvent.SeriesSelectionHandler;
import com.sencha.gxt.chart.client.chart.series.Primitives;
import com.sencha.gxt.chart.client.chart.series.RadarSeries;
import com.sencha.gxt.chart.client.chart.series.SeriesRenderer;
import com.sencha.gxt.chart.client.draw.RGB;
import com.sencha.gxt.chart.client.draw.sprite.Sprite;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.fx.client.Draggable;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.Resizable;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.Resizable.Dir;
import com.sencha.gxt.widget.core.client.box.MessageBox;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.CollapseEvent;
import com.sencha.gxt.widget.core.client.event.ExpandEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.CollapseEvent.CollapseHandler;
import com.sencha.gxt.widget.core.client.event.ExpandEvent.ExpandHandler;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;


public class RadarChart extends Chart<Item> 
{
	/*
	 * Metadata for Line Chart
	 */
	private ArrayList<Item> data;
	private String title;	
	private Window window;
	private ContentPanel panel;
	private VerticalLayoutContainer layout;

	public RadarChart(RadarChart_Dataset dataset, String title) {
		this.data = dataset.getDataset();
		this.title = title;
	}
	
	public interface DataPropertyAccess extends PropertyAccess<Item> {
	    ValueProvider<Item, Double> value0();
	    ValueProvider<Item, String> category();
	    @Path("category")
	    ModelKeyProvider<Item> categoryKey();
	}
	private static final DataPropertyAccess dataAccess = GWT.create(DataPropertyAccess.class);
	
	@Override
	public Widget asWidget(){		
		final ListStore<Item> store = new ListStore<Item>(dataAccess.categoryKey());
		final Chart<Item> chart = new Chart<Item> ();		
		store.addAll(data);
		chart.setStore(store);
		chart.setShadow(true);

	    RadialAxis<Item, String> axis = new RadialAxis<Item, String>();
	    axis.setCategoryField(dataAccess.category());
	    axis.setDisplayGrid(true);
	    chart.addAxis(axis);
	    
	    final RadarSeries<Item> radar = new RadarSeries<Item>();
	    radar.setYField(dataAccess.value0());
	    radar.setStroke(RGB.BLUE);
	    radar.setShowMarkers(true);
	    Sprite marker = Primitives.circle(0, 0, 4);
	    marker.setFill(new RGB(148, 174, 10));
	    radar.setMarkerConfig(marker);
	    radar.setLineRenderer(createRenderer(0));
	    chart.addSeries(radar);
	    
	    radar.addSeriesSelectionHandler(new SeriesSelectionHandler<Item>()
	    {
	    	@Override
	    	public void onSeriesSelection(SeriesSelectionEvent<Item> event) {
	    		/*Fire corresponding action along with series selection*/
	    		MessageBox pop = new MessageBox("Alert",event.getItem().getCategory()+":"+String.valueOf(event.getItem().getValue0()));
	    		RootPanel.get().add(pop);
	    		pop.show();
	    	}	    	
	    });
	    panel = new FramedPanel();
	    panel.getElement().getStyle().setMargin(10, Unit.PX);
	    panel.setCollapsible(true);
	    panel.setHeadingText(title);
	    panel.setPixelSize(400, 320);
	    panel.setBodyBorder(true);
		
	    /*Enable resizing*/
	    final Resizable resize = new Resizable(panel, Dir.E, Dir.SE, Dir.S);
	    resize.setMinHeight(400);
	    resize.setMinWidth(400);
	    new Draggable(panel, panel.getHeader()).setUseProxy(false);

	    panel.addExpandHandler(new ExpandHandler() {
	        @Override
	        public void onExpand(ExpandEvent event) {
	          resize.setEnabled(true);
	        }
	    });
	      panel.addCollapseHandler(new CollapseHandler() {
	        @Override
	        public void onCollapse(CollapseEvent event) {
	          resize.setEnabled(false);
	        }
	    });	      
	    
		chart.setLayoutData(new VerticalLayoutData(1, 1));
		layout = new VerticalLayoutContainer();
		layout.add(chart);
		layout.setLayoutData(new VerticalLayoutData(1, 1));
		initiateWindow();
		  
	    return panel;
	}
	public SeriesRenderer<Item> createRenderer(final int seriesIndex) {
	    return new SeriesRenderer<Item>() {
	    	@Override
		    public void spriteRenderer(Sprite sprite, int index, ListStore<Item> store) {
	    		sprite.setStrokeWidth(2);
		        sprite.setOpacity(0.50);
		        sprite.setFill(RGB.GREEN);
		}};
	}
	/*For zoom-in event of the chart*/
	public void initiateWindow(){
		final VerticalLayoutContainer panelLayout = new VerticalLayoutContainer();
		final ToolBar toolBar = new ToolBar();		
		window = new Window();
	    window.setPixelSize(900, 700);
	    window.setModal(true);
	    window.setBlinkModal(true);	    
	    window.setHeadingText("Radar Chart");
	    window.addHideHandler(new HideHandler() {
	        @Override
	        public void onHide(HideEvent event) {
	            TextButton open = ((Window) event.getSource()).getData("open");
	            open.focus();
	            panelLayout.clear();
	            panelLayout.add(toolBar);
	            panelLayout.add(layout);
				panel.add( panelLayout);
	        }
	    });
	    //Close button at the bottom of window
		TextButton closeBtn = new TextButton("Close");
		closeBtn.addSelectHandler(new SelectHandler() {
			@Override
			public void onSelect(SelectEvent event) {
				panelLayout.clear();
		        panelLayout.add(toolBar);
		        panelLayout.add(layout);
				panel.add(panelLayout);
				window.hide();
			}
		});
		window.addButton(closeBtn);
		window.setFocusWidget(window.getButtonBar().getWidget(0));
		//zoom-in button on toolbar	    
	    TextButton openBtn = new TextButton("Zoom In");
	    openBtn.addSelectHandler(new SelectHandler() {
	    	@Override
	    	public void onSelect(SelectEvent event) {
	    		window.add(layout);
	    		window.show();
	    	}
	    });
	    window.setData("open", openBtn);	    	    
	    toolBar.add(openBtn);
	    toolBar.setLayoutData(new VerticalLayoutData(1, -1));
	    	    
        panelLayout.add(toolBar);
        panelLayout.add(layout);	
		panel.add(panelLayout);
	}

}
